package com.zhxh.nzpos.setting;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.LocalInfoDao;
import com.zhxh.nzpos.model.LocalInfo;

/**
 * 系统设置界面
 * 
 * @author wangzt
 * 
 */
public class SystemSettingActivity extends BaseActivity {

	private LocalInfoDao dao;
	private LocalInfo localInfo;

	private String id = "";
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_system);
		dao = new LocalInfoDao(this);
		initData();
		initViews();
	}

	private void initViews() {
		final EditText postIdET = (EditText) findViewById(R.id.pos_id_et);
		final EditText shopNameET = (EditText) findViewById(R.id.shop_name_et);
		final EditText serverIpET = (EditText) findViewById(R.id.server_ip_et);
		final EditText serverPortET = (EditText) findViewById(R.id.server_port_et);
		final EditText phone_et = (EditText) findViewById(R.id.phone_et);
		
		ImageView okBtn = (ImageView) findViewById(R.id.system_setting_ok_btn);
		
		if(localInfo != null) {
			id = localInfo.getId();
			postIdET.setText(localInfo.getPostId());
			shopNameET.setText(localInfo.getName());
			serverIpET.setText(localInfo.getServerIP());
			serverPortET.setText(localInfo.getServerPort());
			phone_et.setText(localInfo.getPhone());
		}
		okBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO初始化商店信息并保存
				if (postIdET.getText().toString().length() == 0) {
					postIdET.setError("机号不能为空");
					return;

				}
				if (phone_et.getText().toString().length() == 0) {
					phone_et.setError("电话不能为空");
					return;

				}
				if (shopNameET.getText().toString().length() == 0) {
					shopNameET.setError("商店名称不能为空");
					return;

				}
				if (serverIpET.getText().toString().length() == 0) {
					serverIpET.setError("服务器地址不能为空");
					return;

				}
				if (serverPortET.getText().toString().length() == 0) {
					serverPortET.setError("端口不能为空");
					return;

				}
				localInfo = new LocalInfo();
				localInfo.setPostId(postIdET.getText().toString());
				localInfo.setCardID("111");
				localInfo.setName(shopNameET.getText().toString());
				localInfo.setPassword("654123");
				localInfo.setServerIP(serverIpET.getText().toString());
				localInfo.setServerPort(serverPortET.getText().toString());
				localInfo.setPhone(phone_et.getText().toString());
				if(id != ""){
					localInfo.setId(id);
					dao.update(localInfo);
				}
				else {
					dao.save(localInfo);
				}
				
				Toast.makeText(getApplicationContext(), "系统设置成功",
						Toast.LENGTH_SHORT).show();
				// finish();
			}
		});
		/*
		 * ImageView cancelBtn = (ImageView)
		 * findViewById(R.id.system_setting_cancel_btn);
		 * cancelBtn.setOnClickListener(new OnClickListener() { public void
		 * onClick(View v) { finish(); } });
		 */
	}

	private void initData() {
		List<LocalInfo> locals = dao.getAll();
		if (locals.size() > 0)
			localInfo = locals.get(0);
	}
}
